﻿using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;

public partial class WebUserControl_NhanHangVeTuChiNhanh : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    static string error = "";
    string str = BCDataCenter.Configuration.CurrentConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLocation();
            txtNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            BindFindGrid();
        }
        
    }
    protected void btRequest_Click(object sender, EventArgs e)
    {
        BindFindGrid();
    }
    protected void btChon_Click(object sender, EventArgs e)
    {
        txtSoChungTu.Text = "";
        dt = new ClassExecute.ClassExecute();
        string check=dt.GetDataDepend(str,"ls_veridateVoucher",CommandType.StoredProcedure,ref error,
                new SqlParameter("@SONo",txtMaSO.Text.Trim()));
        if (check != "OK")
        {
            uscMsgBox1.AddMessage(check, uscMsgBox.enmMessageType.Error);
        }
        else
        {
            
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getVoucher", CommandType.StoredProcedure,
            new SqlParameter("@mSONo", txtMaSO.Text.Trim()),
            new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));
        if (db.Rows.Count > 0)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
            if (db.Rows[0]["mTrnNo"].ToString() == "Book")
            {

            }
            else
            {
                txtSoChungTu.Text = db.Rows[0]["mTrnNo"].ToString();
                try
                {
                    DropLocation.SelectedValue = db.Rows[0]["mLocation"].ToString();
                }
                catch
                {

                }
            }
        }
        else
        {
            Clear();
        }
      }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string docno="";
        if (txtSoChungTu.Text == "" || txtSoChungTu.Text == null)
        {
            docno = dt.GetDataDepend(str, "K_GetDocno", CommandType.StoredProcedure, ref error);
            txtSoChungTu.Text = docno;
        }
        else
        {
            docno = txtSoChungTu.Text;
        }
        dt.ExecuteSQl_bool(str,CommandType.StoredProcedure,"k_DeleteDuplicateVoucher",ref error,
            new SqlParameter("@docno",docno));

        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
                
                dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_insertVoucher", ref error,
                    new SqlParameter("@location", DropLocation.SelectedValue.ToString()),
                    new SqlParameter("@sitecode", Session["sitecode"].ToString()),
                    new SqlParameter("@docno",docno),
                    new SqlParameter("@itemkey",GridView2.Rows[i].Cells[1].Text),
                    new SqlParameter("@trntyp","R"),
                    new SqlParameter("@docdate",txtNgay.Text),
                    new SqlParameter("@stockqty",GridView2.Rows[i].Cells[3].Text),
                    new SqlParameter("@stockcost",GridView2.Rows[i].Cells[4].Text),
                    new SqlParameter("@SONo",txtMaSO.Text.Trim()));
                
        }
    }
    protected void imb_xoa_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_deleteVoucher", ref error,
            new SqlParameter("@mTrnNo", txtSoChungTu.Text));
        Clear();

    }
    void Clear()
    {
        txtMaSO.Text = "";
        txtSoChungTu.Text = "";
        txtNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        GridView2.DataSource = null;
        GridView2.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindFindGrid();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_findVoucher", CommandType.StoredProcedure,
                new SqlParameter("@mTrnNo", grv.Cells[0].Text),
                new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));
           
            if (db.Rows.Count > 0)
            {
                txtSoChungTu.Text = "";
                GridView2.DataSource = db;
                GridView2.DataBind();
                if (db.Rows[0]["mTrnNo"].ToString() == "Book")
                {

                }
                else
                {
                    txtMaSO.Text = db.Rows[0]["mSONo"].ToString();
                    txtSoChungTu.Text = db.Rows[0]["mTrnNo"].ToString();
                    try
                    {
                        DropLocation.SelectedValue = db.Rows[0]["mLocation"].ToString();
                    }
                    catch
                    {

                    }
                }
                TabContainer1.ActiveTabIndex = 0;
            }
        }
    }  
    void BindLocation()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "K_GetLocation", CommandType.StoredProcedure,
            new SqlParameter("@SiteCode", Session["sitecode"].ToString()));
        DropLocation.DataSource = db;
        DropLocation.DataValueField = "location";
        DropLocation.DataTextField = "description";
        DropLocation.DataBind();
    }
    void BindFindGrid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_loadUnpostedVoucher", CommandType.StoredProcedure,
            new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));
        if (db.Rows.Count > 0)
        {
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
        
    }
}
